package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;
import com.hermit.qmst.tools.ParseXML;
import com.hermit.qmst.tools.Preferences;

/**
 * Created by hermithermit on 15/2/17.
 */
public class ChangePasswordActivity extends Activity implements View.OnClickListener {
    private String TAG="ChangePasswordActivity:";
    private EditText metOldpwd;
    private EditText metNewpwd;
    private EditText metSurepwd;
    private ProgressDialog mProgressDialog;

    private String mOldpwd;
    private String mNewpwd;
    private String mSurepwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passwordset);
        initView();
    }

    private void initView() {

        ((TextView) findViewById(R.id.tv_title)).setText("修改密码");
        ((ImageView) findViewById(R.id.iv_title_left)).setImageResource(R.drawable.ic_back);
        findViewById(R.id.iv_title_left).setOnClickListener(this);


        metOldpwd = (EditText) findViewById(R.id.et_opwd).findViewById(R.id.et_clear);
        metNewpwd = (EditText) findViewById(R.id.et_npwd).findViewById(R.id.et_clear);
        metSurepwd = (EditText) findViewById(R.id.et_spwd).findViewById(R.id.et_clear);
        findViewById(R.id.bt_set).setOnClickListener(this);

    }

    private void request_changpwd() {
        mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
        RequestQueue requestQueue = Volley.newRequestQueue(this);


        String url= Common.submitChangePasswordPath() + "regnum=" + Common.myPhone + "&oldpwd=" + mOldpwd + "&newpwd=" + mNewpwd + "&agent=" + Common.mAgentId;
        Log.i(TAG+"url:", url, true);
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String s) {
                Log.i("ChangePasswordActivity", s, true);
                if (s != null && s.length() > 0) {

                    String code = ParseXML.parseResponseXML(s, "Ret");

                    if (code.equals("0")) {

                        Preferences p = new Preferences(ChangePasswordActivity.this);
                        p.setPreferenceStringValue(Preferences.MY_PASSWORD, mNewpwd);
                        ChangePasswordActivity.this.finish();
                        Toast.makeText(ChangePasswordActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(ChangePasswordActivity.this, "修改失败", Toast.LENGTH_LONG).show();
                    }
                    mProgressDialog.dismiss();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ChangePasswordActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                mProgressDialog.dismiss();
            }
        });


        requestQueue.add(request);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.bt_set:

                mOldpwd = metOldpwd.getText().toString().trim();
                mNewpwd = metNewpwd.getText().toString().trim();
                mSurepwd = metSurepwd.getText().toString().trim();

                if (mOldpwd.length() == 0) {
                    Toast.makeText(ChangePasswordActivity.this, "原密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mNewpwd.length() == 0) {
                    Toast.makeText(ChangePasswordActivity.this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mSurepwd.equals(mNewpwd)) {
                    Toast.makeText(ChangePasswordActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                request_changpwd();
                break;
            case R.id.iv_title_left:
                ChangePasswordActivity.this.finish();
                break;
        }

    }
}
